package com.shujia.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.Objects;

public class BaseUtils {
    private BaseUtils() {}

    public static Connection getConnection(){
        Connection conn = null;
        try {
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hadoop102:2181,hadoop103:2181,hadoop104:2181");
            conn = ConnectionFactory.createConnection(conf);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    public static void printResult(Result result) {
        StringBuilder sb = new StringBuilder();
        String rowKey = Bytes.toString(result.getRow());
        sb.append("行键：").append(rowKey);
        List<Cell> cells = result.listCells(); //获取多个单元格组成的List集合，每个Cell单元格中都包含了四个维度和一个值（行键，列簇，列名，版本号(默认最新)）--> 列值
        for (Cell cell : cells) {
            //使用Hbase自带的工具类CellUtil获取Cell单元格中的维度值
            String lieming = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));

            sb.append(",").append(lieming).append("：").append(value);
        }
        System.out.println(sb);
    }
    public static void createTable(Admin admin, String tableName, byte[] columnFamily, byte[]... args) {
            TableName students = TableName.valueOf(tableName);
            try {
                //先查看表有没有
                boolean b = admin.tableExists(students);
                if (b) {
                    admin.disableTable(students);
                    admin.deleteTable(students);
                }
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(students);
                if (args!=null){
                    for (byte[] cf : args){
                        ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.of(cf);
                        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
                    }
                }
                ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.of("info");
                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
                TableDescriptor td = tableDescriptorBuilder.build();
                admin.createTable(td);
                System.out.println("创建成功!");
            }catch (Exception e) {
                e.printStackTrace();
            }

    }

    public static void printAll(String  tableName, Connection conn,Admin admin){
        TableName students1 = TableName.valueOf(tableName);
        try {
            TableDescriptor descriptor = admin.getDescriptor(students1);
            Table table = conn.getTable(students1);
            ResultScanner scanner = table.getScanner(new Scan());
            scanner.forEach(row -> {
                StringBuffer res = new StringBuffer();
                String rowkey =new String(row.getRow());
                for (byte[] columnFamilyName : descriptor.getColumnFamilyNames()) {
                    NavigableMap<byte[], byte[]> familyMap = row.getFamilyMap(columnFamilyName);
                    res.append(rowkey).append("\t").append(new String(columnFamilyName)).append(":{");
                    familyMap.forEach((k, v) -> {
                        res.append(new String(k)).append("=").append(new String(v)).append(",");
                    });
                    res.delete(res.length() - 1, res.length());
                    res.append("};");
                    System.out.println(res);
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
